module lab2C
{
	uses interface Boot;
	
	uses interface AlohaSend;
	uses interface AlohaReceive;
	uses interface AlohaState;
	
	uses interface Timer<TMilli> as myTimer;

}
implementation
{
	uint8_t g_channel = 7;
	uint8_t g_sequence = 0;
	
	myMsg_t temp_msg = {10,10,10};
	
	uint8_t g_state = STATE_READY;
	
	event void Boot.booted()
	{	
		error_t temp = -1;
		#ifdef D7015E_DEBUG_LAB2C	
		printf("|%d|-----|lab2C::Boot.booted()\n",TOS_NODE_ID);
		printfflush();
		#endif
		call myTimer.startPeriodic(1500);
	}
	
	event void myTimer.fired(){
		
		error_t temp = -1;
		
		
  		switch (g_sequence){
  			case 0:
  				//Init the radio
				temp = call AlohaState.start();
				#ifdef D7015E_DEBUG_LAB2C	
				printf("|%d|-----|lab2C::call AlohaState.start() [%d]\n",TOS_NODE_ID,temp);
				printfflush();
				#endif
				if( temp == SUCCESS){
					atomic g_state = STATE_STARTING;
					atomic g_sequence = -1;	
				}
  				break;
  			case 1:
  			  	//Setting The channel
		  		temp = call AlohaState.setChannel(g_channel);
		  		#ifdef D7015E_DEBUG_LAB2C	
				printf("|%d|-----|lab2C::call setChannel(%d) [%d]\n",TOS_NODE_ID, g_channel,temp);
				printfflush();
				#endif
				if( temp == SUCCESS){
					atomic g_state = STATE_CHANNEL;
					atomic g_sequence = -1;
				}
  				break;
  			case 2:
  				//Turning Radio Off
		  		temp = call AlohaState.stop();
		  		#ifdef D7015E_DEBUG_LAB2C	
				printf("|%d|-----|lab2C::call stop() [%d]\n",TOS_NODE_ID,temp);
				printfflush();
				#endif
				if( temp == SUCCESS){
					atomic g_state = STATE_STOPPING;
					call myTimer.stop();	
				}
  				break;
  			case 3:
				#ifdef D7015E_DEBUG_LAB2C	  	
  				printf("|%d|-----|lab2C::fired() [3]\n",TOS_NODE_ID);
				printfflush();
				#endif
				
  				if(TOS_NODE_ID == 0){
  					
  					//Sending info

					temp_msg.uid = TOS_NODE_ID;
					temp_msg.counter = 0;
					temp_msg.myData = 10;
					
	  				temp = call AlohaSend.send(&temp_msg,1);
					#ifdef D7015E_DEBUG_LAB2C	
					printf("|%d|-----|lab2C::call send() [%d]\n",TOS_NODE_ID,temp);
					printf("|%d|-----|txMsg:: uid [%d]\n",TOS_NODE_ID,temp_msg.uid);
  					printf("|%d|-----|txMsg:: counter [%d]\n",TOS_NODE_ID,temp_msg.counter);
  					printf("|%d|-----|txMsg:: myData [%d]\n",TOS_NODE_ID,temp_msg.myData);
					printfflush();
					#endif
					if( temp == SUCCESS){
						atomic g_state = STATE_TX_SEND;
						atomic g_sequence = -1;	
					}
					#ifdef D7015E_DEBUG_LAB2C	
					printf("|%d|-----|lab2C::call send() [%d]\n",TOS_NODE_ID,temp);
					printfflush();
					#endif
					
  				}/*else if(TOS_NODE_ID == 1){
  					//Receive info
  					temp = call AlohaReceive.receive();
					printf("|%d|-----|lab2C::call receive() [%d]\n",TOS_NODE_ID,temp);
					printfflush();
					
					if( temp == SUCCESS){
						atomic g_state = STATE_RECEIVE;
						atomic g_sequence = -1;	
					}
  					
  				}*/
  				break;
  			default:
  				#ifdef D7015E_DEBUG_LAB2C	
  				printf("|%d|-----|lab2C::fired() Waiting\n",TOS_NODE_ID);
				printfflush();
				#endif
  		}
  		
	}

	/**************************************************************************
	 * 
	 * 			AlohaSend interface
	 * 
	 * 
	 **************************************************************************/
  	//command error_t cancel(message_t* msg);
  	//command error_t send(message_t* msg, am_addr_t destination);
  	
  	event void AlohaSend.sendDone(error_t error){
		#ifdef D7015E_DEBUG_LAB2C	
  		printf("|%d|-----|lab2C:: sendDone() [%d]\n",TOS_NODE_ID,error);
  		printfflush();
		#endif
  		
  		if (error == SUCCESS){
  			atomic g_state = STATE_TURN_ON;
  			atomic g_sequence = 2;
  		}
  		
  	}
  	
  		
	/***************************************************************************
	 * 
	 * 			AlohaReceive interface
	 * 
	 * 
	 **************************************************************************/
	//command error_t receive();
	
    event message_t* AlohaReceive.receiveDone(message_t* txMsg, error_t error){
    	
  		myMsg_t* my_Msg_temp = (myMsg_t*)txMsg;
  		#ifdef D7015E_DEBUG_LAB2C	
  		printf("|%d|-----|lab2C:: receiveDone() [%d]\n",TOS_NODE_ID,error);
  		printf("|%d|-----|txMsg:: uid [%d]\n",TOS_NODE_ID,my_Msg_temp->uid);
  		printf("|%d|-----|txMsg:: counter [%d]\n",TOS_NODE_ID,my_Msg_temp->counter);
  		printf("|%d|-----|txMsg:: myData [%d]\n",TOS_NODE_ID,my_Msg_temp->myData);
  		printfflush();
  		#endif	
  		if (error == SUCCESS){
  			atomic g_state = STATE_TURN_ON;
  			atomic g_sequence = 2;
  			return (message_t*) my_Msg_temp;
  		}else
  			return NULL;
  		
    }
    
    /***************************************************************************
	 * 
	 * 			AlohaState interface
	 * 
	 * 
	 **************************************************************************/
	//command error_t start();
	//command error_t stop();
	//command error_t setChannel(uint8_t channel);
	//command uint8_t getChannel();
	
	event void AlohaState.startDone(error_t error){
		#ifdef D7015E_DEBUG_LAB2C	
	  	printf("|%d|-----|lab2C:: startDone() [%d]\n",TOS_NODE_ID,error);
  		printfflush();
  		#endif
  		//Setting apropiate State
  		if (error == SUCCESS){
  			atomic g_state = STATE_TURN_ON;
  			atomic g_sequence = 1;
  		}
  		

	}
	
	event void AlohaState.stopDone(error_t error){
		#ifdef D7015E_DEBUG_LAB2C	
		printf("|%d|-----|lab2C:: stopDone() [%d]\n",TOS_NODE_ID,error);
  		printfflush();
  		#endif
  		//Setting apropiate State
  		if (error == SUCCESS){
  			atomic g_state = STATE_TURN_OFF;
  			atomic g_sequence = 0;
  		}
  			
  		
	}
	
	event void AlohaState.setChannelDone(error_t error){
		#ifdef D7015E_DEBUG_LAB2C	
		printf("|%d|-----|lab2C:: setChannelDone() [%d]\n",TOS_NODE_ID,error);
  		printfflush();
  		#endif
  		//Setting apropiate State
  		if (error == SUCCESS){
  			atomic g_state = STATE_TURN_ON;
  			atomic g_sequence = 3;
  		}
  		

	}
}